package com.spark.lola.controller.logistics;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.constant.LogisticsConstant;
import com.spark.lola.logistics.entity.LocationInfo;
import com.spark.lola.logistics.service.LocationInfoService;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.wms.configuration.WmsProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/04/27 17:15
 * 货运地址信息接口
 **/
@RestController
@RequestMapping("/api/logistics/location")
@Api(value = "/api/logistics/location", tags = "货运地址信息接口")
public class LocationInfoController extends BaseController {

    @Autowired
    private LocationInfoService locationInfoService;
    @Autowired
    private WmsProperties wmsProperties;

    /**
     * @param key
     * @return
     */
    @RequestMapping(value = "/findLike/{locationType}")
    public ResponseBean map(@PathVariable Integer locationType, @RequestParam String key) {
        return ResponseBean.success(locationInfoService.findLike(locationType, key));
    }

    /**
     * @param key
     * @return
     */
    @RequestMapping(value = "/map")
    public ResponseBean map(@RequestParam("key") String key) {
        return ResponseBean.success(locationInfoService.selectMap(LogisticsConstant.LocationType.PAPER, key));
    }


    @ApiOperation(value = "分页查询货运地址信息")
    @RequiresPermissions(value = "m5130")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findPage(@RequestBody QueryParam<LocationInfo> queryParam, @SessionAttribute ShiroUser shiroUser) {
        // 是否业务员
        if (wmsProperties.getPurchase().getDutyId().get(0).equals(shiroUser.getDutyId())) {
            bindSelfQuery(queryParam, "o.leaderId", shiroUser);
        }
        IPage<LocationInfo> locationInfoIPage = locationInfoService.findPage(queryParam);
        return ResponseBean.success(locationInfoIPage);
    }

    @ApiOperation(value = "获取货运地址详情")
    @ApiImplicitParam(name = "id", value = "货运地址id", required = true, dataType = "long", paramType = "path")
    @RequiresPermissions(value = "m5134")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable long id, @SessionAttribute ShiroUser shiroUser) {
        LocationInfo locationInfo = new LocationInfo();
        if (id != 0) {
            locationInfo = locationInfoService.getById(id);
        }
        return ResponseBean.success(locationInfo);
    }

    @ApiOperation(value = "添加/修改货运地址信息")
    @RequiresPermissions(value = {"m5131", "m5133"}, logical = Logical.OR)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseBean save(@RequestBody LocationInfo locationInfo, @SessionAttribute ShiroUser shiroUser) {
        LocationInfo model = new LocationInfo();
        if (locationInfo.getId() != null) {
            model = locationInfoService.getById(locationInfo.getId());
        } else {
            // 设置初始化项
        }
        DataBeanUtils.buildUpdateBean(model, locationInfo);
        locationInfoService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "删除货运地址信息")
    @ApiImplicitParam(name = "nos", value = "货运地址信息id集合", required = true, dataType = "Long", paramType = "query")
    @RequiresPermissions(value = "m5132")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public ResponseBean del(@RequestParam(value = "nos") List<Long> ids) {
        ids.forEach(id -> {
            locationInfoService.removeById(id);
        });
        return ResponseBean.success();
    }

}
